package model

import (
	"gorm.io/gorm"
	"keep/common/initialize"
	keep "keep/common/proto"
)

type User struct {
	gorm.Model
	Name     string `gorm:"type:varchar(11);not null;index:ip_idx"`
	Password string `gorm:"type:varchar(32);not null;index:ip_idx"`
}

type GroupClass struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(5);not null;index:ip_idx"`
	Price   float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Comment string  `gorm:"type:varchar(50);not null;index:ip_idx"`
	Dates   string  `gorm:"type:varchar(255);not null;index:ip_idx"`
	Time    string  `gorm:"type:varchar(50);not null;index:ip_idx"`
	Stadium string  `gorm:"type:varchar(20);not null;index:ip_idx"`
	Address string  `gorm:"type:varchar(50);not null;index:ip_idx"`
	Num     int64   `gorm:"type:int(10);not null;index:ip_idx"`
	Coach   string  `gorm:"type:varchar(20);not null;index:ip_idx"`
}

func (c GroupClass) FindKeepReservationInfo(name string) (class []*GroupClass, err error) {
	err = initialize.DB.Where("name=?", name).Find(&class).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c GroupClass) FindClassId(id int64) (class *GroupClass, err error) {
	err = initialize.DB.Take(&class).Where("id=?", id).Error
	if err != nil {
		return nil, err
	}
	return
}
func (c GroupClass) FindClass() (class []*GroupClass, err error) {
	err = initialize.DB.Find(&class).Error
	if err != nil {
		return nil, err
	}
	return
}
func (c GroupClass) FindKeepReservation(id int64) (class []*GroupClass, err error) {
	err = initialize.DB.Where("id=?", id).Find(&class).Error
	if err != nil {
		return nil, err
	}
	return
}

type Reservation struct {
	gorm.Model
	UserId       int64   `gorm:"type:int(10);not null;index:ip_idx"`
	GroupClassId int64   `gorm:"type:int(10);not null;index:ip_idx"`
	Sum          int64   `gorm:"type:int(10);not null;index:ip_idx"`
	Price        float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Notes        string  `gorm:"type:varchar(50);not null;index:ip_idx"`
	Status       int     `gorm:"type:int;not null;index:ip_idx"`
}

func (r Reservation) FindReservation() (class []*Reservation, err error) {
	err = initialize.DB.Find(&class).Error
	if err != nil {
		return nil, err
	}
	return
}

func (r Reservation) CreateKeepReservation(in *keep.KeepReservationReq, price float64) (class *Reservation, err error) {

	class = &Reservation{
		UserId:       in.UserId,
		GroupClassId: in.ClassId,
		Sum:          in.Sum,
		Price:        float64(in.Sum) * price,
		Notes:        in.Notes,
		Status:       0,
	}

	err = initialize.DB.Create(&class).Error
	if err != nil {
		return nil, err
	}
	return
}
func (r Reservation) CreateReservation(in *keep.ReservationConfirmReq, price float64) (class *Reservation, err error) {

	class = &Reservation{
		UserId:       in.UserId,
		GroupClassId: in.ClassId,
		Sum:          in.Sum,
		Price:        float64(in.Sum) * price,
		Notes:        in.Notes,
		Status:       0,
	}

	err = initialize.DB.Create(&class).Error
	if err != nil {
		return nil, err
	}
	return
}

func (r Reservation) DelReservations(id int64) (class *Reservation, err error) {
	err = initialize.DB.Model(&class).Where("id=?", id).Delete(&class).Error
	if err != nil {
		return nil, err
	}
	return
}
